﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class discount_product : System.Web.UI.Page
{
    protected DataTable result = new DataTable();
    string dis_id;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        dis_id = this.Request.Params["did"];
        if (dis_id == null || dis_id == "")
        {
            Page.RegisterStartupScript("ss", "<script>alert('当前没有正在为任何优惠添加产品，请录入优惠活动信息后重试!')</script>");
            return;
        }
        
        if (!IsPostBack)
        {
            result.Columns.Add("p_id");
            result.Columns.Add("p_name");
            result.Columns.Add("p_image");
            string name = Session["USERNAME"].ToString();
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            SqlCommand get_product = new SqlCommand();
            get_product.Connection = sc1;
            get_product.CommandText = "select PRODUCT_ID,PRODUCT_NAME,PRODUCT_IMAGE from INFO_PRODUCT where ENTER_ID=@eid1";
            get_product.Parameters.AddWithValue("@eid1", Session["ENTERID"].ToString());
            SqlDataReader r = get_product.ExecuteReader();
            while (r.Read())
            {
                DataRow row = result.NewRow();
                row["p_id"] = r.GetString(r.GetOrdinal("PRODUCT_ID"));
                row["p_name"] = r.GetString(r.GetOrdinal("PRODUCT_NAME"));
                row["p_image"] = r.GetString(r.GetOrdinal("PRODUCT_IMAGE"));
                result.Rows.Add(row); 
                
            }
            
            DataBind();
            sc1.Close();
           
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            string pid = e.CommandArgument.ToString();//获取到需要插入的产品ID
            try
            {//先检验要插入的产品ID和活动ID组合在表中是否存在

                SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
                sc1.Open();
                SqlCommand check_ex = new SqlCommand("select DISCOUNT_ID from DISCOUNT_PRODUCT where DISCOUNT_ID=@didd and PRODUCT_ID=@pidd", sc1);
                check_ex.Parameters.AddWithValue("@didd", dis_id);
                check_ex.Parameters.AddWithValue("@pidd", pid);
                object a = check_ex.ExecuteScalar();
                if (a == null)
                {//不存在
                    SqlCommand insert_pd = new SqlCommand("insert into DISCOUNT_PRODUCT values(@did,@pid)", sc1);
                    insert_pd.Parameters.AddWithValue("@did", dis_id);
                    insert_pd.Parameters.AddWithValue("@pid", pid);
                    while (Application["sqllock"] == "lock")
                    {//检测是否已被锁住
                        System.Threading.Thread.Sleep(2000);
                    }
                    Application["sqllock"] = "lock";//上锁          
                    if (insert_pd.ExecuteNonQuery() == 1)
                    {
                        Application["sqllock"] = "unlock";//解锁
                        Page.RegisterStartupScript("ss", "<script>alert('插入成功!')</script>");
                        return;
                    }
                    else
                    {
                        Application["sqllock"] = "unlock";//解锁
                        Page.RegisterStartupScript("ss", "<script>alert('数据库异常!')</script>");
                        return;
                    }
                }
                else
                {//存在
                    Page.RegisterStartupScript("ss", "<script>alert('您要设置为优惠的产品已经存在了=3=!')</script>");
                    return;
                }
            }
            catch (Exception ex)
            {
                Page.RegisterStartupScript("ss", "<script>alert('数据库错误!"+ex.Message+ex.StackTrace+"')</script>");
                return;
            }
            
        }   
    }
}